#ifndef __SERIALCODEC_H
#define __SERIALCODEC_H

#include <stdint.h>

typedef int16_t (*CHECK_PTR)(uint8_t *, uint16_t);

typedef enum 
{
    SERIAL_PROC_TYPE_SAVE = 0,//what we need 
    SERIAL_PROC_TYPE_COMPARE,//head or tail 
    SERIAL_PROC_TYPE_CHECK,//check sum or check xor or something else
    SERIAL_PROC_TYPE_PACKLEN,//indicate the length of this packet data
    SERIAL_PROC_TYPE_MAX,
}SERIAL_PROC_TYPE_E;

typedef enum 
{
    SERIAL_BYTEORDER_SMALL = 0,
    SERIAL_BYTEORDER_BIG,
    SERIAL_BYTEORDER_MAX,
}SERIAL_BYTEORDER_E;

typedef struct _serial_unit
{
    uint8_t id;//unit sequence
    SERIAL_PROC_TYPE_E type;//how to handle received bytes
    uint16_t unitlen;//how many bytes should be handle by this unit
    struct _serial_unit *next;
    void *ptr;//data or fun
    uint8_t id;//unit sequence
    SERIAL_PROC_TYPE_E type;//how to handle received bytes
    uint16_t unitlen;//how many bytes should be handle by this unit
    struct _serial_unit *next;
    void *ptr;//data or fun
}SERIAL_UNIT_S;

typedef struct 
{
    uint8_t proccnt;
    uint32_t packlen;
    uint8_t *dataptr;
    SERIAL_PROC_TYPE_E curstate;
    SERIAL_UNIT_S *curunit;
}SERIAL_CTRL_S;

typedef struct 
{
    uint8_t procstate;
    uint8_t byteorder;
    uint16_t packfixlen;//packet total len == fix len + variavle len
    uint8_t *buf;
    CHECK_PTR checkfun;
}SERIAL_PROC_S;

void resetSerialProc(void);
int8_t procSerialData(uint8_t databyte);

#endif //__SERIALCODEC_H
